#
# tagEvent.py - an open (no sign-in required) handler page for RFID events from a client
#
# Copyright 2008-2009 Autelic Association (http://www.autelic.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi
from google.appengine.api import mail
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import os
from google.appengine.ext.webapp import template
import logging
import sys
import wsgiref.handlers
import pages

import atom.url
import gdata.service
import gdata.alt.appengine

class handleTagEvent(webapp.RequestHandler):
  def get(self):
      # create HTML page with a welcome message
      page = pages.Page('tagEventor external test page')

      # build a path to the html file, using current directory as starting point
      directory = os.path.dirname(__file__)
      path = os.path.join(directory, 'tagEvent.html')

      # tagEventor will send the user to a url of this form "...tagEvent?sam=$1&uid=$2&type=$3"
      # where $1 is SAM_serial of reader, $2 is unique ID of tag, and $3 is "IN" or "OUT"
      # extract those three variables from the url, avoiding the "type" keyword of python!?
      sam = self.request.get('sam')
      uid = self.request.get('uid')
      event = self.request.get('type')

      template_values = {'sam': sam, 
                         'uid': uid,
                         'type': event}

      # show html page that displays the passed parameters in the URL
      page.add_html(template.render(path, template_values))

      #add a link to the internal version of the same page
#      internal_link = 
      page.add_html('<a href="in/tagEvent?sam=%s&uid=%s&type=%s">Pass the tagEvent to the internal version of the page (Sign in required)</a><br><br>' % (sam, uid, event) )

      self.response.out.write(page.show())

# End of 'handleTagEvent'
########################################################
    
def main(argv):
  application = webapp.WSGIApplication(
    [('/tagEvent', handleTagEvent)],
    debug=True)
    
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main(sys.argv)
